using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;

using ECCIT.Web.Configuration;

namespace ECCIT.Web.UI.WebControls
{
	/// <summary>
	/// Summary description for ELPButton.
	/// </summary>
	[DefaultProperty("Text"),
       ToolboxData("<{0}:YUIButton runat=server></{0}:YUIButton>")]
	public class YUIButton : System.Web.UI.WebControls.Button
	{
		private string _text;
		private string _paddingLeft;
		private string _imgSrc;	
		private string _onClick;
		private string _type;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get {return (_text==null) ? string.Empty : _text ;}
			set {_text = value;}
		}

		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("2em")] 
		public string PaddingLeft
		{
			get {return (_paddingLeft==null) ? "2em" : _paddingLeft;}
			set {_paddingLeft = value;}
		}

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string ImgSrc
		{
			get {return (_imgSrc==null) ? string.Empty : _imgSrc ;}
			set {_imgSrc = value;}
		}

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public new string OnClick
		{
			get {return (_onClick==null) ? string.Empty : _onClick;}
			set {_onClick = value;}
		}

        [Bindable(true),
        Category("Appearance"),
        DefaultValue("")]
        public string MarginLeft { get; set; }

		public string Type
		{
			get {return (_type == null) ? string.Empty : _type; }	
			set {_type = value;}
		}

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			// render style
			StringBuilder styleBuilder = new StringBuilder("<style type=\"text/css\">");
			styleBuilder.Append(".yui-button#").Append(this.ID).Append(" button {\n");

            string virtualDir = (this.Page is IBasePage) ? ((IBasePage)this.Page).ApplicationVirtualDirectory : "../";
			string imgPath = (this.ImgSrc.Trim() == string.Empty) ? "" : this.ImgSrc;
			int index = imgPath.LastIndexOf("/");
			if (index > -1)
				imgPath = virtualDir + "resources/images/" + imgPath.Substring(index+1);

			if (this.Text.Trim() == string.Empty)
			{
				styleBuilder.Append("text-indent:-4em;\n");
				styleBuilder.Append("overflow:hidden;\n");
				styleBuilder.Append("width:").Append(this.Width).Append(";\n");
				styleBuilder.Append("margin-left:").Append("4em").Append(";\n");
				styleBuilder.Append("padding:").Append("0 1.75em").Append(";\n");
				styleBuilder.Append("background: url(").Append(imgPath).Append(") center center no-repeat;\n");
				styleBuilder.Append("font-family:").Append("Courier New").Append(";\n");
				styleBuilder.Append("font-size:").Append("9pt").Append(";\n");
				styleBuilder.Append("}\n");
			}
			else
			{
				styleBuilder.Append("border-top:0px;\n");
				styleBuilder.Append("border-left:0px;\n");
				styleBuilder.Append("padding-left:").Append(this.PaddingLeft).Append(";\n");
				styleBuilder.Append("background: url(").Append(imgPath).Append(") 10% 50% no-repeat;\n");
				styleBuilder.Append("font-family:").Append("Courier New").Append(";\n");
				styleBuilder.Append("font-size:").Append("9pt").Append(";\n");
				styleBuilder.Append("}\n");
			}

			styleBuilder.Append("</style>\n");
			output.Write(styleBuilder.ToString());

			// render control
			output.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
			output.AddAttribute(HtmlTextWriterAttribute.Type, "Button");
			output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
            output.AddAttribute(HtmlTextWriterAttribute.Style, "margin-left:" + this.MarginLeft);
			//output.RenderBeginTag(HtmlTextWriterTag.Input);
			//output.RenderEndTag();
			base.Render(output);

			// render scripts
			StringBuilder scriptBuilder = new StringBuilder("<script type=\"text/javascript\">\n<!--\n");			
			scriptBuilder.Append("var ").Append(this.ID).Append(" = new YAHOO.widget.Button(\"").Append(this.ID).Append("\");\n");
			if (this.OnClick.Trim() != string.Empty)
				scriptBuilder.Append(this.ID).Append(".on(\"click\", ").Append(this.OnClick).Append(");\n");
			
			if (string.Compare(this.Type, "Accept", true) == 0)
			{
				string funcName = this.ID + "_enterKeyClick";
				scriptBuilder.Append("function ").Append(funcName).Append("(){\n");
				scriptBuilder.Append("if (event.keyCode==13){\n");
				scriptBuilder.Append("event.cancelBubble = true;\nevent.returnValue=false;\n");
				scriptBuilder.Append(this.OnClick).Append("();\n}\n}");
				scriptBuilder.Append("document.body.onkeypress = ").Append(funcName).Append(";");
			}
			scriptBuilder.Append("//-->\n</script>");
			output.Write(scriptBuilder.ToString());
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			string scriptKey = "ELPButton_Script";
			if (!this.Page.IsClientScriptBlockRegistered(scriptKey))
			{					
				string registerScript = BuildStyleSheetBlock((string[])StyleSheetManager.StyleSheets["Button"]);
				registerScript += BuildClientScriptBlock((string[])JavaScriptManager.Scripts["Button"]);
				this.Page.RegisterClientScriptBlock(scriptKey, registerScript);
			}
		}

		private string BuildStyleSheetBlock(string[] styleSheets)
		{
			string styleSheetBlock = string.Empty;
			if (styleSheets != null)
			{
                string virtualDir = (this.Page is IBasePage) ? ((IBasePage)this.Page).ApplicationVirtualDirectory : "../";
				foreach (string styleSheet in styleSheets)
					styleSheetBlock += "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + virtualDir + styleSheet + "\">";
			}

			return styleSheetBlock;
		}

		private string BuildClientScriptBlock(string[] clientScripts)
		{
			string clientScriptBlock = string.Empty;
			if (clientScripts != null)
			{
                string virtualDir = (this.Page is IBasePage) ? ((IBasePage)this.Page).ApplicationVirtualDirectory : "../";
				foreach (string clientScript in clientScripts)
					clientScriptBlock += "<script language=javascript src='" + virtualDir + clientScript + "'></script>";
			}

			return clientScriptBlock;
		}
	}
}
